package com.flyoil.petromp.mvp_m.CourseMessageModel;

import com.flyoil.petromp.Constant.ConstantNetworkCode;
import com.flyoil.petromp.R;
import com.flyoil.petromp.apiservice.OnObserverRetrofitResetCallBack;
import com.flyoil.petromp.base.BaseEntity;
import com.flyoil.petromp.entity.entity_course_message.GoodsDemandMessageEntity;
import com.flyoil.petromp.entity.entity_order.MapListEntity;
import com.flyoil.petromp.entity.entity_order.OrderFilesEntity;
import com.flyoil.petromp.entity.entity_order.OrderGoodsEntity;
import com.flyoil.petromp.entity.entity_order.OrderSignatureEntity;
import com.flyoil.petromp.base.BaseModel;
import com.flyoil.petromp.mvp_p.Listener.OnPresenterListener;
import com.flyoil.petromp.mvp_p.Listener.OrderMessageListener;
import com.flyoil.petromp.utils.MoccaPreferences;
import com.flyoil.petromp.utils.OrderUtl.OrderStatusUtil;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by songzhiyin on 2017/10/31.
 * 物资需求单详情界面的数据处理类
 */

public class GoodsDemandMessageModel implements BaseModel {


    @Override
    public void loadNetworkData(Map<String, Object> map, final OnPresenterListener onPresenterListener) {
        httpModel.getCourseGoodsDemandMessage(map, new OnObserverRetrofitResetCallBack<BaseEntity>() {
            @Override
            public void onSuccess(BaseEntity model) {
                super.onSuccess(model);
                if (model.getCode() != ConstantNetworkCode.SuccedCode) {
                    onPresenterListener.onsucced(null);
                    return;
                }
                GoodsDemandMessageEntity entity = null;
                if (model instanceof GoodsDemandMessageEntity) {
                    entity = (GoodsDemandMessageEntity) model;

                }
                if (entity != null && entity.getData() != null) {
                    OrderMessageListener listener = (OrderMessageListener) onPresenterListener;
                    //基本信息
                    listener.upDataTitleName(R.mipmap.message_basic_icon,"基本信息");
                    List<MapListEntity> mapListEntities = new ArrayList<>();
                    mapListEntities.add(new MapListEntity("计划主题", entity.getData().getPlanSubject()));
                    mapListEntities.add(new MapListEntity("计划编号", entity.getData().getPlanSn()));
                    mapListEntities.add(new MapListEntity("需求名称", entity.getData().getDemandName()));
                    mapListEntities.add(new MapListEntity("需求编号", entity.getData().getSn()));
                    mapListEntities.add(new MapListEntity("申请人", entity.getData().getApplicantName()));
                    mapListEntities.add(new MapListEntity("申请部门", entity.getData().getDeptName()));
                    mapListEntities.add(new MapListEntity("使用单位", entity.getData().getUseDept()));
                    mapListEntities.add(new MapListEntity("申请日期", entity.getData().getSubmitDateLabel()));
                    listener.upDataTextsView(mapListEntities);
                    //相关材料
                    if (entity.getData().getFiles() != null) {
                        listener.upDataImgsView("相关材料", entity.getData().getFiles());
                    }
                    //设置单据明细的标题
                    listener.upDataTitleName(R.mipmap.message_project_icon,"需求明细");
                    //需求明细
                    List<OrderGoodsEntity> orderGoodsEntities = new ArrayList<>();
                    if (entity.getData().getDemandItems() != null) {
                        for (int i = 0; i < entity.getData().getDemandItems().size(); i++) {
                            List<MapListEntity> mapListEntities1 = new ArrayList<>();
                            mapListEntities1.add(new MapListEntity("物资名称", entity.getData().getDemandItems().get(i).getMaterialName()));
                            mapListEntities1.add(new MapListEntity("规格/型号", entity.getData().getDemandItems().get(i).getModel()));
                            orderGoodsEntities.add(new OrderGoodsEntity(entity.getData().getDemandItems().get(i).getId(), mapListEntities1));
                        }
                    }
                    listener.upDataGoodsView(orderGoodsEntities);

                    //签字意见并获取签字意见列表未录入意见的数据最大的id
                    if (entity.getData().getConfirmLogs() != null) {
                        List<OrderSignatureEntity> confirmLogs = new ArrayList<OrderSignatureEntity>();
                        int sigantureId = 0;
                        for (int i = 0; i < entity.getData().getConfirmLogs().size(); i++) {
                            OrderSignatureEntity sigantureEntity = entity.getData().getConfirmLogs().get(i);
                            if (sigantureEntity.getAppliedTimeLabel() != null && sigantureEntity.getAppliedTimeLabel().length() > 0) {
                                confirmLogs.add(sigantureEntity);
                            } else {
                                if (sigantureEntity.getId() < sigantureId || sigantureId == 0) {
                                    sigantureId = sigantureEntity.getId();
                                }
                            }
                        }
                        //更新签字意见列表
                        listener.upDataSiganturesView(confirmLogs);
                        //更新单据的签字意见的id
                        listener.upDataOrderConfirmId(sigantureId);
                    }
                    //审核状态为待审核，单据状态流转中，显示签字意见输入框
                    if (entity.getData().getTransformStatusLabel().equals(OrderStatusUtil.FLOW_FLOW) && entity.getData().getConfirmStatusLabel().equals(OrderStatusUtil.STATUS_UNAPPROVE)) {
                        listener.upDataShowSigantureView(true);
                    } else {
                        listener.upDataShowSigantureView(false);
                    }
                    //审核状态为已驳回待撤销、单据流转状态是待撤销、当前登录用户是申请人，才能显示撤销按钮
                    if (entity.getData().getApplicantId() == MoccaPreferences.userId.get()&&entity.getData().getTransformStatusLabel().equals(OrderStatusUtil.FLOW_UNBACKOUT) && entity.getData().getConfirmStatusLabel().equals(OrderStatusUtil.STATUS_UNBACKOUT)) {
                        listener.upDataShowBackoutView(true);
                    } else {
                        listener.upDataShowBackoutView(false);
                    }
                    //审核状态为已撤销时，显示已撤销图标
                    if (entity.getData().getConfirmStatusLabel().equals(OrderStatusUtil.STATUS_BACKOUTED)) {
                        listener.upDataShowRemoveImagView(true);
                    } else {
                        listener.upDataShowRemoveImagView(false);
                    }

                } else {
                    onPresenterListener.onsucced(null);
                }
            }

            @Override
            public void onFailure(String msg) {
                onPresenterListener.onsucced(null);
//                setdata(onPresenterListener);
            }
        });


    }

    private void setdata(final OnPresenterListener onPresenterListener) {
        OrderMessageListener listener = (OrderMessageListener) onPresenterListener;
        List<MapListEntity> mapListEntities = new ArrayList<>();
        listener.upDataTitleName(R.mipmap.message_basic_icon,"基本信息");
        mapListEntities.add(new MapListEntity("计划主题", "物资采购计划申请"));
        mapListEntities.add(new MapListEntity("计划编号", "CP2017102701"));
        mapListEntities.add(new MapListEntity("需求名称", "电脑采购需求单"));
        mapListEntities.add(new MapListEntity("需求编号", "SX25455555111"));
        mapListEntities.add(new MapListEntity("申请人", "王华"));
        mapListEntities.add(new MapListEntity("申请部门", "北京康特尔有限公司"));
        mapListEntities.add(new MapListEntity("使用单位", "技术部"));
        mapListEntities.add(new MapListEntity("申请日期", "2017年8月18日"));
        listener.upDataTextsView(mapListEntities);

        List<OrderFilesEntity> imgUrl = new ArrayList<>();
        imgUrl.add(new OrderFilesEntity("http://p0.so.qhimgs1.com/dmfd/326_204_/t012c852c8f981d6a00.jpg"));
        listener.upDataImgsView("相关材料", imgUrl);

        List<OrderGoodsEntity> orderGoodsEntities = new ArrayList<>();
        List<MapListEntity> mapListEntities1 = new ArrayList<>();
        mapListEntities1.add(new MapListEntity("物资名称", "摄像机"));
        mapListEntities1.add(new MapListEntity("规格/型号", "WX2541"));
        orderGoodsEntities.add(new OrderGoodsEntity(mapListEntities1));
        listener.upDataGoodsView(orderGoodsEntities);

        List<OrderSignatureEntity> orderSignatureEntities = new ArrayList<>();
        orderSignatureEntities.add(new OrderSignatureEntity("王华"));
        orderSignatureEntities.add(new OrderSignatureEntity("李薇"));
        orderSignatureEntities.add(new OrderSignatureEntity("李薇"));
        orderSignatureEntities.add(new OrderSignatureEntity("赵莉"));
        listener.upDataSiganturesView(orderSignatureEntities);

        listener.upDataShowSigantureView(false);

        listener.upDataShowRemoveImagView(false);

        listener.upDataTitleName(R.mipmap.matter_goods_order_icon,"需求明细");
    }
}
